<?php

namespace App\Http\Responses;

use Illuminate\Http\JsonResponse;

class CustomResponse
{
    /**
     * 快捷返回成功
     *
     * @return JsonResponse
     */
    public static function ok($msg = '')
    {
        return self::makeResponse(0, '操作成功', []);
    }

    /**
     * 返回成功
     *
     * @return JsonResponse
     */
    public static function success($data = [], $msg = '操作成功')
    {
        return self::makeResponse(0, $msg, $data, $statusCode = 200);
    }

    /**
     * 快捷操作失败
     *
     * @return JsonResponse
     */
    public static function fail($msg = '操作失败', $data = [])
    {
        return self::makeResponse(400, '操作失败', []);
    }

    /**
     * 操作失败
     *
     * @return JsonResponse
     */
    public static function error($msg = '操作失败', $data = [])
    {
        return self::makeResponse(400, $msg, $data, $statusCode = 400);
    }

    protected static function makeResponse($status, $msg, $data, $statusCode = 200)
    {
        $responseData = [
            'status' => $status,
            'msg' => $msg,
            'data' => $data,
        ];

        return response()->json($responseData, $statusCode);
    }
}
